/**
 *
 */
package common.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import common.util.Constants;

/**
 * @author jond.liao
 *
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport 
{
	protected static final String DEFAULT_CHARACTER = Constants.DEFAULT_CHARACTER;

	protected static final String ContentType_HTML = "text/html";
	protected static final String ContentType_XML = "text/xml";
	protected static final String ContentType_JSON = "application/json";

	protected Logger log = Logger.getLogger(this.getClass());
	private static ApplicationContext ac;// spring application context
	
	static
	{
		ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml",
				"daoContext.xml"});
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String bean_id)
	{
		return (T)ac.getBean(bean_id);
	}
	/**
	 * get project root directory.
	 *
	 * @return
	 */
	protected String getWebRealPath() {
		return ServletActionContext.getServletContext().getRealPath("/");
	}

	/**
	 * get request of parameter value.
	 *
	 * @param parameter
	 * @return string parameter value,if this value is null then return "".
	 */
	protected String getParameter(String parameter) {
		Object value = getParameters().get(parameter);
		if (value != null) {
			return ((String[]) value)[0];
		}
		return "";
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	/**
	 * get request of parameter values.
	 *
	 * @param parameter
	 * @return string parameter values.
	 */
	protected String[] getParameterValues(String parameter) {
		String[] values = this.getRequest().getParameterValues(parameter);
		if (values != null) {
			return values;
		}
		return new String[] {};
	}

	/**
	 * get request of int parameter value.
	 *
	 * @param parameter
	 * @return int
	 */
	protected int getIntParameter(String parameter) {
		Object value = getParameters().get(parameter);
		if (value != null) {
			try {
				return Integer.parseInt(((String[]) value)[0]);
			} catch (Exception e) {
				return 0;
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	protected Map getParameters() {
		ActionContext actionContext = ActionContext.getContext();
		Map parameters = (Map) actionContext.getParameters();
		return parameters;
	}

	/**
	 * get web deployment Path.
	 *
	 * @return
	 */
	public String getContextPath() {
		return ServletActionContext.getServletContext().getRealPath("/");
	}

	/**
	 * The information directly back to the browser,Default character is utf-8.
	 *
	 * @param content
	 * @throws IOException
	 */
	protected void sendClient(String content) {
		this.sendClient(content, DEFAULT_CHARACTER, ContentType_HTML);
	}
	/**
	 * The information directly back to the browser.
	 *
	 * @param content
	 * @param character
	 * @throws IOException
	 */
	protected void sendClient(String content, String character)
			throws IOException {
		sendClient(content, character, ContentType_XML);
	}
	/**
	 * The information directly back to the browser.
	 *
	 * @param content
	 * @param character
	 * @throws IOException
	 */
	protected void sendClient(String content, String character, String contentType)
			{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType(String.format("%s;charset=%s", contentType, character));
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(content);
			out.flush();
			out.close();
		} catch (IOException e) {}

	}
	/***
	 * Send xml data to client, utf-8 encoding
	 * @param content
	 * @throws IOException
	 */
	protected void sendClientXML(String content) throws IOException {
		this.sendClient(content, DEFAULT_CHARACTER, ContentType_XML);
	}

	/***
	 * Send JSON data to client, utf-8 encoding
	 * @param content
	 * @throws IOException
	 */
	protected void sendClientJSON(String content) throws IOException {
		this.sendClient(content, DEFAULT_CHARACTER, ContentType_JSON);
	}

	/**
	 *
	 * @param queryString
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Map getParameterMap(String queryString) {
		if (queryString == null || "".equals(queryString))
			return null;
		Map parameterMap = new HashMap();
		String[] parArray = queryString.split("&");
		for (int i = 0; i < parArray.length; i++) {
			if (parArray[i].split("=").length == 2) {
				parameterMap.put(parArray[i].split("=")[0], parArray[i]
						.split("=")[1]);
			}
		}
		return parameterMap;
	}
}
